package com.sdp.irrigation.controller;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.redis.RedisUtil;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.cache.EqValveCache;
import com.sdp.irrigation.cache.IrrigationCacheKeys;
import com.sdp.irrigation.db.irrigation_eq_valve.service.IIrrigationEqValveService;
import com.sdp.irrigation.entity.IrrigationEqValve;
import com.sdp.irrigation.entity.IrrigationPumpWell;
import com.sdp.irrigation.utils.IrrigationConstants;

/**
 * <p>
 * 设备阀门信息 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/IrrigationEqValveController")
public class IrrigationEqValveController {

	@Autowired
	private IIrrigationEqValveService iIrrigationEqValveService;
	
	@Autowired
	private Query query;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@Autowired
	private EqValveCache eqValveCache;
	
	@Autowired
	private RedisUtil redisUtil;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iIrrigationEqValveService, IrrigationEqValve.class, dataCenter));
	}
	
	@PostMapping(value = "/list")
	public Result<?> list(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.list(iIrrigationEqValveService, IrrigationEqValve.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody IrrigationEqValve eqValve) {
		if(eqValve==null) {
			return Result.error("设备阀门信息为空");
		}
		if(StringUtils.isEmpty(eqValve.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iIrrigationEqValveService, IrrigationEqValve.class, new String[] {"eqNo","serialNo"}, "keyId", eqValve, null, true);
			if(r==null || !r.isSuccess()) {
				return Result.error("同一个设备上的阀门序号不能重复");
			}
			eqValve.setSwitchStatus(IrrigationConstants.阀门开关状态_关.getCode());
			eqValve.setCreateTime(LocalDateTime.now());
			eqValve.setUpdateTime(LocalDateTime.now());
			iIrrigationEqValveService.save(eqValve);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iIrrigationEqValveService, IrrigationEqValve.class, new String[] {"eqNo","serialNo"}, "keyId", null, eqValve, true);
			if(r==null || !r.isSuccess()) {
				return Result.error("同一个设备上的阀门序号不能重复");
			}
			eqValve.setUpdateTime(LocalDateTime.now());
			iIrrigationEqValveService.updateById(eqValve);
		}
		
		//更新阀门缓存
		eqValveCache.updateEqValveCache(eqValve.getEqNo(), eqValve.getEqNo(), iIrrigationEqValveService.queryEqValveMapByEqNo(eqValve.getEqNo()));
		
		return Result.ok();
	}
	
	@PostMapping(value = "/initValve")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> initValve(@RequestBody IrrigationPumpWell pumpWel) {
		if(pumpWel==null) {
			return Result.error("机井信息为空");
		}
		if (StringUtils.isEmpty(pumpWel.getEqNo())) {
			return Result.error("设备编号为空");
		}
		
		QueryWrapper<IrrigationEqValve> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(IrrigationEqValve::getEqNo, pumpWel.getEqNo());
		iIrrigationEqValveService.remove(queryWrapper);
		redisUtil.del(IrrigationCacheKeys.getValveRealTimeCacheKey(pumpWel.getEqNo()));
		
		List<IrrigationEqValve> insertList = new ArrayList<>();
		for (int i = 0; i < 26; i++) {
			IrrigationEqValve eqValve = new IrrigationEqValve();
			eqValve.setEqNo(pumpWel.getEqNo());
//			eqValve.setValveNo(String.valueOf(i + 1));
			eqValve.setSerialNo(i + 1);
			eqValve.setValveName(String.valueOf(eqValve.getSerialNo()));
			eqValve.setSwitchStatus(IrrigationConstants.阀门开关状态_关.getCode());
			eqValve.setCreateTime(LocalDateTime.now());
			eqValve.setUpdateTime(LocalDateTime.now());
			insertList.add(eqValve);
		}
		
		iIrrigationEqValveService.saveBatch(insertList);
		return Result.ok();
	}
}

